<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    :destroyOnClose="true"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="分类父id" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-tree-select
            ref="treeSelect"
            placeholder="请选择分类父id"
            v-decorator="['parentId', validatorRules.parentId]"
            dict="lb_portal_category,name,id"
            pidField="parent_id"
            pidValue="0"
            hasChildField="has_child">
          </j-tree-select>
        </a-form-item>
        <a-form-item label="分类文章数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['postCount', validatorRules.postCount]" placeholder="请输入分类文章数" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['status', validatorRules.status]" placeholder="请输入状态" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="删除时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['deleteTime', validatorRules.deleteTime]" placeholder="请输入删除时间" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['listOrder', validatorRules.listOrder]" placeholder="请输入排序" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="分类名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['name', validatorRules.name]" placeholder="请输入分类名称"></a-input>
        </a-form-item>
        <a-form-item label="分类描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['description']" rows="4" placeholder="请输入分类描述"/>
        </a-form-item>
        <a-form-item label="分类层级关系路径" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['path', validatorRules.path]" placeholder="请输入分类层级关系路径"></a-input>
        </a-form-item>
        <a-form-item label="seoTitle" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['seoTitle', validatorRules.seoTitle]" placeholder="请输入seoTitle"></a-input>
        </a-form-item>
        <a-form-item label="seoKeywords" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['seoKeywords', validatorRules.seoKeywords]" placeholder="请输入seoKeywords"></a-input>
        </a-form-item>
        <a-form-item label="seoDescription" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['seoDescription', validatorRules.seoDescription]" placeholder="请输入seoDescription"></a-input>
        </a-form-item>
        <a-form-item label="分类列表模板" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['listTpl', validatorRules.listTpl]" placeholder="请输入分类列表模板"></a-input>
        </a-form-item>
        <a-form-item label="分类文章页模板" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['oneTpl', validatorRules.oneTpl]" placeholder="请输入分类文章页模板"></a-input>
        </a-form-item>
        <a-form-item label="扩展属性" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['more']" rows="4" placeholder="请输入扩展属性"/>
        </a-form-item>
        <a-form-item label="父级节点" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['pid']" placeholder="请输入父级节点"></a-input>
        </a-form-item>
        <a-form-item label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择创建时间" v-decorator="['createTime']" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </a-form-item>
        
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  
  import JTreeSelect from '@/components/jeecg/JTreeSelect'
  
  export default {
    name: "LbPortalCategoryModal",
    components: { 
      JDate,
      JTreeSelect
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
          parentId: {
            rules: [
              { required: true, message: '请输入分类父id!'},
            ]
          },
          postCount: {
            rules: [
              { required: true, message: '请输入分类文章数!'},
            ]
          },
          status: {
            rules: [
              { required: true, message: '请输入状态!'},
            ]
          },
          deleteTime: {
            rules: [
              { required: true, message: '请输入删除时间!'},
            ]
          },
          listOrder: {
            rules: [
              { required: true, message: '请输入排序!'},
            ]
          },
          name: {
            rules: [
              { required: true, message: '请输入分类名称!'},
            ]
          },
          path: {
            rules: [
              { required: true, message: '请输入分类层级关系路径!'},
            ]
          },
          seoTitle: {
            rules: [
              { required: true, message: '请输入seoTitle!'},
            ]
          },
          seoKeywords: {
            rules: [
              { required: true, message: '请输入seoKeywords!'},
            ]
          },
          seoDescription: {
            rules: [
              { required: true, message: '请输入seoDescription!'},
            ]
          },
          listTpl: {
            rules: [
              { required: true, message: '请输入分类列表模板!'},
            ]
          },
          oneTpl: {
            rules: [
              { required: true, message: '请输入分类文章页模板!'},
            ]
          },
        },
        url: {
          add: "/bobo.cms/lbPortalCategory/add",
          edit: "/bobo.cms/lbPortalCategory/edit",
        },
        expandedRowKeys:[],
        pidField:"parentId"
     
      }
    },
    created () {
    },
    methods: {
      add (obj) {
        this.edit(obj);
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'parentId','postCount','status','deleteTime','listOrder','name','description','path','seoTitle','seoKeywords','seoDescription','listTpl','oneTpl','more','pid','createTime'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let old_pid = this.model[this.pidField]
            let formData = Object.assign(this.model, values);
            let new_pid = this.model[this.pidField]
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                this.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'parentId','postCount','status','deleteTime','listOrder','name','description','path','seoTitle','seoKeywords','seoDescription','listTpl','oneTpl','more','pid','createTime'))
      },
      submitSuccess(formData,flag){
        if(!formData.id){
          let treeData = this.$refs.treeSelect.getCurrTreeData()
          this.expandedRowKeys=[]
          this.getExpandKeysByPid(formData[this.pidField],treeData,treeData)
          this.$emit('ok',formData,this.expandedRowKeys.reverse());
        }else{
          this.$emit('ok',formData,flag);
        }
      },
      getExpandKeysByPid(pid,arr,all){
        if(pid && arr && arr.length>0){
          for(let i=0;i<arr.length;i++){
            if(arr[i].key==pid){
              this.expandedRowKeys.push(arr[i].key)
              this.getExpandKeysByPid(arr[i]['parentId'],all,all)
            }else{
              this.getExpandKeysByPid(pid,arr[i].children,all)
            }
          }
        }
      }
      
      
    }
  }
</script>